import http from '@/utils/http'
import type { IProduct, IProductQuery } from '@/types'

//新增
export const productAdd = (payload: IProduct) => {
    return http.post('/api/manage/product', payload)
}
//修改
export const productEdit = (payload: IProduct) => {
    return http.put(`/api/manage/product`, payload)
}
//新增或修改
export const productEditOrAdd = (payload: IProduct) => {
    if (payload.id) {
        return productEdit(payload)
    } else {
        return productAdd(payload)
    }
}
//删除
export const productDel = (id: number) => {
    return http.delete(`/api/manage/product/${id}`)
}
//查询
export const productList = (query:IProductQuery) => {
    return http.get('/api/manage/product/page', { params: query })
}
//根据id查询一个
export const productById = (id: number) => {
    return http.get(`/api/manage/product/${id}`)
}

export const productBatchDel = (delIds: number[]) => {
    const delTasks: Promise<IProduct>[] = delIds.map(id => http.delete(`/api/manage/product/${id}`))
    return Promise.all(delTasks)
}